#ifndef WORD_H

typedef char *String;

typedef struct {
    int count;
    String *words;
} Virtual_Word;


String A0[] = {"and", "as", "about", "above", "across", "at", "also", "although", "an", "around", "along", "always", "ahead", "after", "any", "among", "a", "away"};
String B1[] = {"by", "before", "between", "beyond", "behind", "because"};
String C2[] = {"could", "can"};
String D3[] = {"down", "during", "despite"};
String E4[] = {"ever", "every", "each", "even"};
String F5[] = {"from", "for"};
String H7[] = {"have"};
String I8[] = {"in"};
String M12[] = {"many", "much", "more", "may", "might", "must"};
String O14[] = {"on", "over", "other", "only", "out", "off", "of"};
String S18[] = {"so", "some", "such", "since"};
String T19[] = {"that", "then", "the", "than", "through", "throughout", "that", "these", "this", "those", "there", "therefore", "till", "to"};
String U20[] = {"under", "until", "upon", "up"};
String W22[] = {"with", "what", "when", "would", "who", "whom", "whose", "which", "where", "why", "without", "whether"};

Virtual_Word virtual_word_sheet[26] = {
    { // a 0
        .count = 18,
    },
    { // b 1
        .count = 6,
    },
    { // c 2
        .count = 2,
    },
    { // d 3
        .count = 3,
    },
    { // e 4
        .count = 4,
    },
    { // f 5
        .count = 2,
    },
    { // g 6
        .count = 0,
    },
    { // h 7
        .count = 1,
    },
    { // i 8
        .count = 1,
    },
    { // j 9
        .count = 0,
    },
    { // k 10
        .count = 0,
    },
    { // l 11
        .count = 0,
    },
    { // m 12
        .count = 6,
    },
    { // n 13
        .count = 0,
    },
    { // o 14
        .count = 7,
    },
    { // p 15
        .count = 0,
    },
    { // q 16
        .count = 0,
    },
    { // r 17
        .count = 0,
    },
    { // s 18
        .count = 4,
    },
    { // t 19
        .count = 14,
    },
    { // u 20
        .count = 4,
    },
    { // v 21
        .count = 0,
    },
    { // w 22
        .count = 12,
    },
    { // x 23
        .count = 0,
    },
    { // y 24
        .count = 0,
    },
    { // z 25
        .count = 0,
    }
};

void virtual_word_sheet_init() {
    virtual_word_sheet[0].words = A0;
    virtual_word_sheet[1].words = B1;
    virtual_word_sheet[2].words = C2;
    virtual_word_sheet[3].words = D3;
    virtual_word_sheet[4].words = E4;
    virtual_word_sheet[5].words = F5;
    virtual_word_sheet[7].words = H7;
    virtual_word_sheet[8].words = I8;
    virtual_word_sheet[12].words = M12;
    virtual_word_sheet[14].words = O14;
    virtual_word_sheet[18].words = S18;
    virtual_word_sheet[19].words = T19;
    virtual_word_sheet[20].words = U20;
    virtual_word_sheet[22].words = W22;
}

/**
 * 根据叙词表判断是不是虚词
 */
String virtual_word(String str) {
    if (strlen(str) == 0) {
        return "";
    }
    int index = str[0] - 97;
    Virtual_Word vw = virtual_word_sheet[index];
    if (vw.count == 0) {
        return str;
    }
    for (int i = 0; i < vw.count; ++i) {
        if (strcmp(vw.words[i], str) == 0) {
            return "";
        }
    }
    return str;
}

/**
 * 合并单复数形式, 但不考虑不规则变化
 */
String singular_and_plural(String str) {
    int length = strlen(str);
    if (length == 0) {  // 空字符串直接返回
        return "";
    }
    if (str[length - 1] != 's' || str[length - 2] == 's') {   // 不以 s 结尾或以 ss 结尾直接返回
        return str;
    }
    if (length == 2) {  // 两个字母的直接返回
        return str;
    }
    if (str[length - 2] == 'e') {   // es 结尾
        if (str[length - 3] == 's' ||   // ses
            str[length - 3] == 'x' ||   // xes
            (str[length - 4] == 's') && (str[length - 3] == 'h') || // shes
            (str[length - 4] == 'c') && (str[length - 3] == 'h')) { // ches
            str[length - 2] = '\0';
            return str;
        }
        if (str[length - 3] == 'i') {   // ies 结尾
            str[length - 3] = 'y';
            str[length - 2] = '\0';
            return str;
        }
    }
    str[length - 1] = '\0';
    return str;
}

String past_tense(String str) {
    int length = strlen(str);
    if (length == 0) {  // 空字符串直接返回
        return "";
    }
    if (str[length - 2] != 'e' || str[length - 1] != 'd') { // 不以 ed 结尾的单词直接返回
        return str;
    }
    if (str[length - 3] == 'i') {   // ied
        str[length - 3] = 'y';
        str[length - 2] = '\0';
    }
    if (str[length - 3] == str[length - 4] &&
        (str[length - 5] == 'a' || str[length - 5] == 'e' || str[length - 5] == 'i' || str[length - 5] == 'o' || str[length - 5] == 'u')) {   // ??ed -> stopped
        str[length - 3] = '\0';
        return str;
    }
    str[length - 2] = '\0';
    return str;
}

String init_word(String str) {
    String result = (String) malloc (sizeof(char) * strlen(str));
    int index = 0;
    int valid = 0;
    for (; index < strlen(str); index++) {
        char ch = str[index];
        if (ch >= 'A' && ch <= 'Z') {
            ch += 32;

        } else if (ch < 'a' || ch > 'z') {
            valid++;
            continue;
        }
        result[index - valid] = ch;
    }
    result[index - valid] = '\0';
    return result;
}

String word_handle(String str) {
    String result = init_word(str);
    result = virtual_word(result);
    result = singular_and_plural(result);
    result = past_tense(result);
    return result;
}
#endif